#include "pigs.h"
#include "birds.h"
#include <vector>
#include <fstream>
#include <iomanip>
#include <string>

int main(int argc, char *argv[]){
	float tmpxp, tmpyp, tmpxs, tmpys;		//temporary x/y position, x/y speeds
	string tmpname;							//temporary name
	float tmpr, tmpspd;						//temporary radius, pop speed
	const float tstep = .025;				//constant time increments
	float time;								//time tracker
	ofstream output(argv[3]);				//read from input line
	ifstream inBirds(argv[2]);
	ifstream inPigs(argv[1]);

	vector <Bird> blist;											//save imported birds and pigs
	vector <Pig> plist;
	vector <Pig> popped;											//track popped pigs to end if not all birds are fired yet

	while (!inBirds.eof()){											//import birds
		inBirds >> tmpname >> tmpxp >> tmpyp >> tmpxs >> tmpys;
		Bird tmpbird(tmpname, tmpxp, tmpyp, tmpxs, tmpys);
		blist.push_back(tmpbird);
	}
	while (!inPigs.eof()){											//import pigs
		inPigs >> tmpname >> tmpxp >> tmpyp >> tmpr >> tmpspd;
		Pig tmppig(tmpname, tmpxp, tmpyp, tmpr, tmpspd);
		plist.push_back(tmppig);
	}

	plist.pop_back();												//removes a copy of the last bird/pig from the vector
	blist.pop_back();
	
	for(int i=0; i<blist.size(); i++)								//birds are not stopped by default
		blist[i].stop(false);	
	for(int i=0; i<plist.size(); i++)								//pigs are not popped by default
		plist[i].set_popped(false);

	output << "\n";						//apparently there needs to be an empty line before everything else

	bool collide;													//will detect
	for (int i=0; i<blist.size(); i++){
		time = 0;													//all birds start at time 0
		blist[i].print_launch(output);
		while(!blist[i].no_velocity()){								//next bird if current bird is stopped
			collide = false;
			for(int k=0; k<plist.size(); k++){
				if(plist[k].is_popped())							//ignore already popped pigs
					continue;
				if(plist[k].strikes_the_pig(blist[i])){
					collide = true;									//there was a collision
					plist[k].set_popped(plist[k].pops_the_pig(blist[i]));
					if(plist[k].is_popped()){						//bird popped pig
						plist[k].print_pop(output, time, blist[i]);
						popped.push_back(plist[k]);
					}
					else{											//pig stopped bird
						plist[k].print_stop(output, time, blist[i]);
						break;
					}
				}
			}
			if(blist[i].no_velocity())						//breaks infinite loop where bird is stuck inside of pig
				break;
			time += tstep;									//increment time
			blist[i].move(tstep);							//next step in movement
			if(blist[i].no_velocity() && !collide){			//if the bird stopped without running into a pig, output corresponding line
				blist[i].print_stopped(output, time);
				break;
				}
			}
		if(popped.size()==plist.size())						//end if all pigs are popped
			break;
	}

	vector <Pig> unpopped;									//list unpopped pigs
	for(int i=0; i<plist.size(); i++){
		if (!plist[i].is_popped())
			unpopped.push_back(plist[i]);
	}
	
	if(unpopped.size()>0){									//ouput unpopped pigs
		output << "Unpopped pigs:\n";
		for (int k=0; k<unpopped.size(); k++)
			output << "  " << unpopped[k].get_name() << "\n";
	}
	else													//congratulations for not missing
		output << "Congratulations!  All pigs were popped.";

	return 0;
}
